<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>js-闭包</title>
    <link rel="shortcut icon" href="../images/图标.png" type="image/x-icon">
   <style>
       body{
            background-image: url(../images/1.jpg);
            background-repeat: no-repeat;
            background-position: center;
            background-attachment: fixed;
            background-size: 100%;
        }
        .head{
            margin: 20px;
            background-color: white;
            padding: 10px;
        }
        .s1{
            font-size: 18px;
            font-weight: bold;
        }
        h4{
            font-size: 30px;
            margin-top: 15px;
            margin-bottom: 10px;
        }
        .p1{
            margin-left: 20px;
            margin-top: 0;
        }
        p{
            width: 70%;
            font-size: 18px;
        }
        img{
            display: block;
        }
        .d1{
            color:#D4D4D4;background-color: #2B3A42;
    width: 960px;
    border-radius: 6px;
    padding-left: 15px;
    padding: 10px;
    
   text-indent: 16px;
   font-size:17px;line-height:16px;
   white-space: nowrap;
   font-family:Consolas;
    margin-bottom: 30px;
        }
        .head{
            margin: 20px;
            background-color: white;
            padding: 10px;
        }
 .head{
            position: relative;
        }
        .liuyan{
            width: 100px;
            height: 30px;
            font-size: 25px;
            font-weight: bold;
            margin-left: 30px;
            text-align: center;
            border-bottom: 2px solid black;  
           
        }
        .changyan{
            background-color: red;
            font-size: 20px;
            
            width: 108px;
            height: 30px;
           position: absolute;
           left: 86%;
           margin-top: 166px;
           text-align: center;
           border-radius: 10px;
           
          
        }
   </style>
</head>
<body>
    <div class="head"><span class="s1">您现在的位置是：</span>Sharing(来自转载)
        <br>
        <hr>
        <h4>js-闭包</h4>
        <p class="p1">2021-4-14</p>
    <p>
        闭包说白了就是 在函数里面定义一个函数 通过这个函数可以访问外部函数的方法和变量</br>
在es6之前javascript是只有全局作用域和局部作用域 es6新增了块作用域</br>
凡是在函数外定义变量的都是全局变量 在函数内部必须用var声明才是局部变量(es6之前)
    </p>
    <div class="d1">
        <div>
			<span style="font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#569CD6;font-size:14px;">function</span><span style="font-size:14px;">&nbsp;</span><span style="color:#DCDCAA;font-size:14px;">fn1</span><span style="font-size:14px;">(){</span>
		</div>
		<div>
			<span style="font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#569CD6;font-size:14px;">var</span><span style="font-size:14px;">&nbsp;</span><span style="color:#9CDCFE;font-size:14px;">person</span><span style="font-size:14px;">&nbsp;=&nbsp;</span><span style="color:#CE9178;font-size:14px;">"张三"</span><span style="font-size:14px;">;</span>
		</div>
		<div>
			<span style="font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#569CD6;font-size:14px;">var</span><span style="font-size:14px;">&nbsp;</span><span style="color:#9CDCFE;font-size:14px;">age</span><span style="font-size:14px;">&nbsp;=&nbsp;</span><span style="color:#B5CEA8;font-size:14px;">18</span><span style="font-size:14px;">;</span>
		</div>
		<div>
			<span style="font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#569CD6;font-size:14px;">function</span><span style="font-size:14px;">&nbsp;</span><span style="color:#DCDCAA;font-size:14px;">fn2</span><span style="font-size:14px;">(){</span>
		</div>
		<div>
			<span style="font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#4EC9B0;font-size:14px;">console</span><span style="font-size:14px;">.</span><span style="color:#DCDCAA;font-size:14px;">log</span><span style="font-size:14px;">(</span><span style="color:#9CDCFE;font-size:14px;">age</span><span style="font-size:14px;">+</span><span style="color:#9CDCFE;font-size:14px;">person</span><span style="font-size:14px;">);</span>
		</div>
		<div>
			<span style="font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span>
		</div>
		<div>
			<span style="font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#C586C0;font-size:14px;">return</span><span style="font-size:14px;">&nbsp;</span><span style="color:#DCDCAA;font-size:14px;">fn2</span><span style="font-size:14px;">();&nbsp;</span><span style="color:#6A9955;font-size:14px;">//&nbsp;fn2就是一个闭包函数，因为他可以访问到fn1函数的作用域</span>
		</div>
		<div>
			<span style="font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp;}</span>
		</div>
		<div>
			<span style="font-size:14px;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#569CD6;font-size:14px;">var</span><span style="font-size:14px;">&nbsp;</span><span style="color:#9CDCFE;font-size:14px;">x</span><span style="font-size:14px;">&nbsp;=&nbsp;</span><span style="color:#DCDCAA;font-size:14px;">fn1</span><span style="font-size:14px;">();</span><span style="font-family:&quot;font-size:14px;"></span>
		</div>
    </div>
    </div>
    <div class="head">
        <div class="liuyan">
            文章留言
            </div>
 <div class="changyan">
     畅言一下
 </div>
 
 <!--PC版-->
 <div id="SOHUCS" sid="2" style="z-index: 10;width: 90%;"></div>
 <script charset="utf-8" type="text/javascript" src="https://cy-cdn.kuaizhan.com/upload/changyan.js" ></script>
 <script type="text/javascript">
 window.changyan.api.config({
 appid: 'cyvrowqwg',
 conf: 'prod_e0b8d92cf33d2a2e8e19f18a3e4414ec'
 });
 </script>
 
 </div>
</body>
</html>